/*
Copyright (C) 2018-2019 de4dot@gmail.com

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#![allow(unused_results)]

use super::super::super::iced_constants::IcedConstants;
use super::super::super::test_utils::from_str_conv::*;
use super::super::super::*;
use super::op_code_test_case::*;
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::iter::IntoIterator;
use core::u32;
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::io::{BufReader, Lines};
use std::path::Path;

pub(super) struct OpCodeInfoTestParser {
	filename: String,
	lines: Lines<BufReader<File>>,
}

impl OpCodeInfoTestParser {
	pub(super) fn new(filename: &Path) -> Self {
		let display_filename = filename.display().to_string();
		let file = File::open(filename).unwrap_or_else(|_| panic!("Couldn't open file {}", display_filename));
		let lines = BufReader::new(file).lines();
		Self { filename: display_filename, lines }
	}
}

impl IntoIterator for OpCodeInfoTestParser {
	type Item = OpCodeInfoTestCase;
	type IntoIter = IntoIter;

	fn into_iter(self) -> Self::IntoIter {
		// GENERATOR-BEGIN: EncodingKindDict
		// ⚠️This was generated by GENERATOR!🦹‍♂️
		let mut to_encoding_kind: HashMap<&'static str, EncodingKind> = HashMap::with_capacity(5);
		to_encoding_kind.insert("legacy", EncodingKind::Legacy);
		to_encoding_kind.insert("VEX", EncodingKind::VEX);
		to_encoding_kind.insert("EVEX", EncodingKind::EVEX);
		to_encoding_kind.insert("XOP", EncodingKind::XOP);
		to_encoding_kind.insert("3DNow!", EncodingKind::D3NOW);
		// GENERATOR-END: EncodingKindDict

		// GENERATOR-BEGIN: MandatoryPrefixDict
		// ⚠️This was generated by GENERATOR!🦹‍♂️
		let mut to_mandatory_prefix: HashMap<&'static str, MandatoryPrefix> = HashMap::with_capacity(5);
		to_mandatory_prefix.insert("", MandatoryPrefix::None);
		to_mandatory_prefix.insert("NP", MandatoryPrefix::PNP);
		to_mandatory_prefix.insert("66", MandatoryPrefix::P66);
		to_mandatory_prefix.insert("F3", MandatoryPrefix::PF3);
		to_mandatory_prefix.insert("F2", MandatoryPrefix::PF2);
		// GENERATOR-END: MandatoryPrefixDict

		// GENERATOR-BEGIN: OpCodeTableKindDict
		// ⚠️This was generated by GENERATOR!🦹‍♂️
		let mut to_op_code_table_kind: HashMap<&'static str, OpCodeTableKind> = HashMap::with_capacity(7);
		to_op_code_table_kind.insert("legacy", OpCodeTableKind::Normal);
		to_op_code_table_kind.insert("0F", OpCodeTableKind::T0F);
		to_op_code_table_kind.insert("0F38", OpCodeTableKind::T0F38);
		to_op_code_table_kind.insert("0F3A", OpCodeTableKind::T0F3A);
		to_op_code_table_kind.insert("X8", OpCodeTableKind::XOP8);
		to_op_code_table_kind.insert("X9", OpCodeTableKind::XOP9);
		to_op_code_table_kind.insert("XA", OpCodeTableKind::XOPA);
		// GENERATOR-END: OpCodeTableKindDict

		IntoIter { filename: self.filename, lines: self.lines, line_number: 0, to_encoding_kind, to_mandatory_prefix, to_op_code_table_kind }
	}
}

pub(super) struct IntoIter {
	filename: String,
	lines: Lines<BufReader<File>>,
	line_number: u32,
	to_encoding_kind: HashMap<&'static str, EncodingKind>,
	to_mandatory_prefix: HashMap<&'static str, MandatoryPrefix>,
	to_op_code_table_kind: HashMap<&'static str, OpCodeTableKind>,
}

impl Iterator for IntoIter {
	type Item = OpCodeInfoTestCase;

	fn next(&mut self) -> Option<Self::Item> {
		loop {
			match self.lines.next() {
				None => return None,
				Some(info) => {
					let result = match info {
						Ok(line) => {
							self.line_number += 1;
							if line.is_empty() || line.starts_with('#') {
								continue;
							}
							self.read_next_test_case(line, self.line_number)
						}
						Err(err) => Err(err.to_string()),
					};
					match result {
						Ok(tc) => {
							if let Some(tc) = tc {
								return Some(tc);
							} else {
								continue;
							}
						}
						Err(err) => panic!("Error parsing OpCodeInfo test case file '{}', line {}: {}", self.filename, self.line_number, err),
					}
				}
			}
		}
	}
}

// GENERATOR-BEGIN: OpCodeInfoKeys
// ⚠️This was generated by GENERATOR!🦹‍♂️
lazy_static! {
	pub(super) static ref TO_OP_CODE_INFO_KEYS: HashMap<&'static str, u32> = {
		let mut h = HashMap::with_capacity(3);
		h.insert("g", OpCodeInfoKeys::GROUP_INDEX);
		h.insert("op", OpCodeInfoKeys::OP_CODE_OPERAND_KIND);
		h.insert("tt", OpCodeInfoKeys::TUPLE_TYPE);
		h
	};
}

pub(crate) struct OpCodeInfoKeys;
#[allow(dead_code)]
impl OpCodeInfoKeys {
	pub(crate) const GROUP_INDEX: u32 = 0;
	pub(crate) const OP_CODE_OPERAND_KIND: u32 = 1;
	pub(crate) const TUPLE_TYPE: u32 = 2;
}
// GENERATOR-END: OpCodeInfoKeys

// GENERATOR-BEGIN: OpCodeInfoFlags
// ⚠️This was generated by GENERATOR!🦹‍♂️
lazy_static! {
	pub(super) static ref TO_OP_CODE_INFO_FLAGS: HashMap<&'static str, u32> = {
		let mut h = HashMap::with_capacity(36);
		h.insert("notinstr", OpCodeInfoFlags::NOT_INSTRUCTION);
		h.insert("16b", OpCodeInfoFlags::BIT16);
		h.insert("32b", OpCodeInfoFlags::BIT32);
		h.insert("64b", OpCodeInfoFlags::BIT64);
		h.insert("fwait", OpCodeInfoFlags::FWAIT);
		h.insert("o16", OpCodeInfoFlags::OPERAND_SIZE16);
		h.insert("o32", OpCodeInfoFlags::OPERAND_SIZE32);
		h.insert("o64", OpCodeInfoFlags::OPERAND_SIZE64);
		h.insert("a16", OpCodeInfoFlags::ADDRESS_SIZE16);
		h.insert("a32", OpCodeInfoFlags::ADDRESS_SIZE32);
		h.insert("a64", OpCodeInfoFlags::ADDRESS_SIZE64);
		h.insert("LIG", OpCodeInfoFlags::LIG);
		h.insert("L0", OpCodeInfoFlags::L0);
		h.insert("L1", OpCodeInfoFlags::L1);
		h.insert("L128", OpCodeInfoFlags::L128);
		h.insert("L256", OpCodeInfoFlags::L256);
		h.insert("L512", OpCodeInfoFlags::L512);
		h.insert("WIG", OpCodeInfoFlags::WIG);
		h.insert("WIG32", OpCodeInfoFlags::WIG32);
		h.insert("W0", OpCodeInfoFlags::W0);
		h.insert("W1", OpCodeInfoFlags::W1);
		h.insert("b", OpCodeInfoFlags::BROADCAST);
		h.insert("er", OpCodeInfoFlags::ROUNDING_CONTROL);
		h.insert("sae", OpCodeInfoFlags::SUPPRESS_ALL_EXCEPTIONS);
		h.insert("k", OpCodeInfoFlags::OP_MASK_REGISTER);
		h.insert("knz", OpCodeInfoFlags::REQUIRE_NON_ZERO_OP_MASK_REGISTER);
		h.insert("z", OpCodeInfoFlags::ZEROING_MASKING);
		h.insert("lock", OpCodeInfoFlags::LOCK_PREFIX);
		h.insert("xacquire", OpCodeInfoFlags::XACQUIRE_PREFIX);
		h.insert("xrelease", OpCodeInfoFlags::XRELEASE_PREFIX);
		h.insert("rep", OpCodeInfoFlags::REP_PREFIX);
		h.insert("repe", OpCodeInfoFlags::REPE_PREFIX);
		h.insert("repne", OpCodeInfoFlags::REPNE_PREFIX);
		h.insert("bnd", OpCodeInfoFlags::BND_PREFIX);
		h.insert("ht", OpCodeInfoFlags::HINT_TAKEN_PREFIX);
		h.insert("notrack", OpCodeInfoFlags::NOTRACK_PREFIX);
		h
	};
}

pub(crate) struct OpCodeInfoFlags;
#[allow(dead_code)]
impl OpCodeInfoFlags {
	pub(crate) const NOT_INSTRUCTION: u32 = 0;
	pub(crate) const BIT16: u32 = 1;
	pub(crate) const BIT32: u32 = 2;
	pub(crate) const BIT64: u32 = 3;
	pub(crate) const FWAIT: u32 = 4;
	pub(crate) const OPERAND_SIZE16: u32 = 5;
	pub(crate) const OPERAND_SIZE32: u32 = 6;
	pub(crate) const OPERAND_SIZE64: u32 = 7;
	pub(crate) const ADDRESS_SIZE16: u32 = 8;
	pub(crate) const ADDRESS_SIZE32: u32 = 9;
	pub(crate) const ADDRESS_SIZE64: u32 = 10;
	pub(crate) const LIG: u32 = 11;
	pub(crate) const L0: u32 = 12;
	pub(crate) const L1: u32 = 13;
	pub(crate) const L128: u32 = 14;
	pub(crate) const L256: u32 = 15;
	pub(crate) const L512: u32 = 16;
	pub(crate) const WIG: u32 = 17;
	pub(crate) const WIG32: u32 = 18;
	pub(crate) const W0: u32 = 19;
	pub(crate) const W1: u32 = 20;
	pub(crate) const BROADCAST: u32 = 21;
	pub(crate) const ROUNDING_CONTROL: u32 = 22;
	pub(crate) const SUPPRESS_ALL_EXCEPTIONS: u32 = 23;
	pub(crate) const OP_MASK_REGISTER: u32 = 24;
	pub(crate) const REQUIRE_NON_ZERO_OP_MASK_REGISTER: u32 = 25;
	pub(crate) const ZEROING_MASKING: u32 = 26;
	pub(crate) const LOCK_PREFIX: u32 = 27;
	pub(crate) const XACQUIRE_PREFIX: u32 = 28;
	pub(crate) const XRELEASE_PREFIX: u32 = 29;
	pub(crate) const REP_PREFIX: u32 = 30;
	pub(crate) const REPE_PREFIX: u32 = 31;
	pub(crate) const REPNE_PREFIX: u32 = 32;
	pub(crate) const BND_PREFIX: u32 = 33;
	pub(crate) const HINT_TAKEN_PREFIX: u32 = 34;
	pub(crate) const NOTRACK_PREFIX: u32 = 35;
}
// GENERATOR-END: OpCodeInfoFlags

impl IntoIter {
	#[cfg_attr(feature = "cargo-clippy", allow(clippy::len_zero))]
	fn read_next_test_case(&self, line: String, line_number: u32) -> Result<Option<OpCodeInfoTestCase>, String> {
		let elems: Vec<_> = line.split(',').collect();
		if elems.len() != 8 {
			return Err(format!("Invalid number of commas: {}", elems.len() - 1));
		}

		let mut tc = OpCodeInfoTestCase::default();
		tc.line_number = line_number;
		tc.is_instruction = true;
		tc.group_index = -1;

		if is_ignored_code(elems[0].trim()) {
			return Ok(None);
		}
		tc.code = to_code(elems[0].trim())?;
		tc.encoding = self.to_encoding(elems[1].trim())?;
		tc.mandatory_prefix = self.to_mandatory_prefix(elems[2].trim())?;
		tc.table = self.to_table(elems[3].trim())?;
		tc.op_code = Self::to_op_code(elems[4].trim())?;
		tc.op_code_string = String::from(elems[5].trim());
		tc.instruction_string = elems[6].trim().replace('|', ",");

		let mut got_vector_length = false;
		let mut got_w = false;
		for part in elems[7].split_whitespace() {
			let mut key = part.trim();
			if key.is_empty() {
				continue;
			}
			let kv_parts: Vec<_> = key.splitn(2, '=').collect();
			if kv_parts.len() == 2 {
				key = kv_parts[0];
				let value = kv_parts[1];
				match *(*TO_OP_CODE_INFO_KEYS).get(key).unwrap_or(&u32::MAX) {
					OpCodeInfoKeys::GROUP_INDEX => {
						tc.group_index = to_i32(value)?;
						if tc.group_index > 7 {
							return Err(format!("Invalid group index: {}", value));
						}
						tc.is_group = true;
					}

					OpCodeInfoKeys::OP_CODE_OPERAND_KIND => {
						let op_parts: Vec<_> = value.split(';').collect();
						tc.op_count = op_parts.len() as u32;
						if op_parts.len() >= 1 {
							tc.op0_kind = to_op_code_operand_kind(op_parts[0])?;
						}
						if op_parts.len() >= 2 {
							tc.op1_kind = to_op_code_operand_kind(op_parts[1])?;
						}
						if op_parts.len() >= 3 {
							tc.op2_kind = to_op_code_operand_kind(op_parts[2])?;
						}
						if op_parts.len() >= 4 {
							tc.op3_kind = to_op_code_operand_kind(op_parts[3])?;
						}
						if op_parts.len() >= 5 {
							tc.op4_kind = to_op_code_operand_kind(op_parts[4])?;
						}
						const_assert_eq!(5, IcedConstants::MAX_OP_COUNT);
						if op_parts.len() >= 6 {
							return Err(format!("Invalid number of operands: '{}'", value));
						}
					}

					OpCodeInfoKeys::TUPLE_TYPE => tc.tuple_type = to_tuple_type(value.trim())?,

					_ => return Err(format!("Invalid key: '{}'", key)),
				}
			} else {
				assert_eq!(1, kv_parts.len());
				match *(*TO_OP_CODE_INFO_FLAGS).get(key).unwrap_or(&u32::MAX) {
					OpCodeInfoFlags::NOT_INSTRUCTION => tc.is_instruction = false,
					OpCodeInfoFlags::BIT16 => tc.mode16 = true,
					OpCodeInfoFlags::BIT32 => tc.mode32 = true,
					OpCodeInfoFlags::BIT64 => tc.mode64 = true,
					OpCodeInfoFlags::FWAIT => tc.fwait = true,
					OpCodeInfoFlags::OPERAND_SIZE16 => tc.operand_size = 16,
					OpCodeInfoFlags::OPERAND_SIZE32 => tc.operand_size = 32,
					OpCodeInfoFlags::OPERAND_SIZE64 => tc.operand_size = 64,
					OpCodeInfoFlags::ADDRESS_SIZE16 => tc.address_size = 16,
					OpCodeInfoFlags::ADDRESS_SIZE32 => tc.address_size = 32,
					OpCodeInfoFlags::ADDRESS_SIZE64 => tc.address_size = 64,

					OpCodeInfoFlags::LIG => {
						tc.is_lig = true;
						got_vector_length = true;
					}

					OpCodeInfoFlags::L0 => {
						tc.l = 0;
						got_vector_length = true;
					}

					OpCodeInfoFlags::L1 => {
						tc.l = 1;
						got_vector_length = true;
					}

					OpCodeInfoFlags::L128 => {
						tc.l = 0;
						got_vector_length = true;
					}

					OpCodeInfoFlags::L256 => {
						tc.l = 1;
						got_vector_length = true;
					}

					OpCodeInfoFlags::L512 => {
						tc.l = 2;
						got_vector_length = true;
					}

					OpCodeInfoFlags::WIG => {
						tc.is_wig = true;
						got_w = true;
					}

					OpCodeInfoFlags::WIG32 => {
						tc.w = 0;
						tc.is_wig32 = true;
						got_w = true;
					}

					OpCodeInfoFlags::W0 => {
						tc.w = 0;
						got_w = true;
					}

					OpCodeInfoFlags::W1 => {
						tc.w = 1;
						got_w = true;
					}

					OpCodeInfoFlags::BROADCAST => tc.can_broadcast = true,
					OpCodeInfoFlags::ROUNDING_CONTROL => tc.can_use_rounding_control = true,
					OpCodeInfoFlags::SUPPRESS_ALL_EXCEPTIONS => tc.can_suppress_all_exceptions = true,
					OpCodeInfoFlags::OP_MASK_REGISTER => tc.can_use_op_mask_register = true,

					OpCodeInfoFlags::REQUIRE_NON_ZERO_OP_MASK_REGISTER => {
						tc.can_use_op_mask_register = true;
						tc.require_non_zero_op_mask_register = true;
					}

					OpCodeInfoFlags::ZEROING_MASKING => tc.can_use_zeroing_masking = true,
					OpCodeInfoFlags::LOCK_PREFIX => tc.can_use_lock_prefix = true,
					OpCodeInfoFlags::XACQUIRE_PREFIX => tc.can_use_xacquire_prefix = true,
					OpCodeInfoFlags::XRELEASE_PREFIX => tc.can_use_xrelease_prefix = true,
					OpCodeInfoFlags::REP_PREFIX | OpCodeInfoFlags::REPE_PREFIX => tc.can_use_rep_prefix = true,
					OpCodeInfoFlags::REPNE_PREFIX => tc.can_use_repne_prefix = true,
					OpCodeInfoFlags::BND_PREFIX => tc.can_use_bnd_prefix = true,
					OpCodeInfoFlags::HINT_TAKEN_PREFIX => tc.can_use_hint_taken_prefix = true,
					OpCodeInfoFlags::NOTRACK_PREFIX => tc.can_use_notrack_prefix = true,

					_ => return Err(format!("Invalid key: '{}'", key)),
				}
			}
		}
		match tc.encoding {
			EncodingKind::Legacy | EncodingKind::D3NOW => {}
			EncodingKind::VEX | EncodingKind::EVEX | EncodingKind::XOP => {
				if !got_vector_length {
					return Err(String::from("Missing vector length: L0/L1/L128/L256/L512/LIG"));
				}
				if !got_w {
					return Err(String::from("Missing W bit: W0/W1/WIG/WIG32"));
				}
			}
		}

		Ok(Some(tc))
	}

	fn to_encoding(&self, value: &str) -> Result<EncodingKind, String> {
		self.to_encoding_kind.get(value).cloned().ok_or_else(|| format!("Invalid encoding value: '{}'", value))
	}

	fn to_mandatory_prefix(&self, value: &str) -> Result<MandatoryPrefix, String> {
		self.to_mandatory_prefix.get(value).cloned().ok_or_else(|| format!("Invalid mandatory prefix value: '{}'", value))
	}

	fn to_table(&self, value: &str) -> Result<OpCodeTableKind, String> {
		self.to_op_code_table_kind.get(value).cloned().ok_or_else(|| format!("Invalid opcode table value: '{}'", value))
	}

	fn to_op_code(value: &str) -> Result<u32, String> {
		match u32::from_str_radix(value, 16) {
			Ok(value) => Ok(value),
			Err(_) => Err(format!("Invalid opcode: '{}'", value)),
		}
	}
}
